import PageTemplate from '@/components/common/PageTemplate'
import {getdelegateusersettingresult,deldelegateusersetting} from '@/api/api.js'
import * as Constant from '@/utils/constant'
 
export default {
  components:{
    PageTemplate
  },
  data(){
    return {
      brand:[
        {name:'lang.workbench'},
        {name:'lang.work_delegate'}
      ],
      hasIcon:false,
      delegateList:[

      ],
      currentPage:1,
      pageSize:10,
      total:0,
      showCollapse:'collapse1',
      showTabs:'delegate_work',
      nowShowTabs:'delegate_work',//控制 委托和受托刷新
      delegate_man:'',//委托人/受托人
      delegateType:'1',
      flow_type:'',
      flow_status:'',//状态
      vaild_time:null,//有效期
      status:[
        {label:'可用',value:'1',key:'1'},
        {label:'不可用',value:'0',key:'0'},
      ]
    }
  },
  methods: {
    //切换tab页
    toggleTab(tab){
      if(this.nowShowTabs != tab.name){
        this.nowShowTabs = tab.name;
        
        if(this.showTabs == "delegate_work"){
          this.delegateType = 1;
          //委托
          let params = {
            pageIndex:1,
            pageSize:this.pageSize,
            type:1  //1 表示委托 2表示受托
          }
          this.showDelegateList(params);
        }else{
          //受托
          this.delegateType = 2;
          let params = {
            pageIndex:1,
            pageSize:this.pageSize,
            type:2 //1 表示委托 2表示受托
          }
          this.showDelegateList(params);
        }
      }
    },
    addDelegate:function(){
      this.$router.push(Constant.ROUTE_WORK_DELEGATE_ADD.index);
    },
    showDelegateList:function(params){
      this.delegateList = [];
      let _this = this;
      getdelegateusersettingresult(params).then((result) => {
        if(result.status == "200" && result.data.code == "0"){
          let list = result.data.data.data.resultData;
          let page = result.data.data.data.page;
          _this.delegateList = list;
          _this.currentPage = page.startPage;
          _this.pageSize = page.pageSize;
          _this.total = page.totalCount;

        }else{
          _this.$alert(result.data.msg,_this.$t('cm.tips'));
        }
      }).catch((err) => {
        _this.$alert(err,_this.$t('cm.tips'));
      });

    },
    resetData:function(){

    },
    handleSizeChange:function(nowPage){
      let params = {
        pageIndex: this.currentPage = nowPage,
        pageSize: this.pageSize,
        type:this.delegateType
      };
      this.showDelegateList(params);
    },
    handleCurrentChange:function(nowPage){
      let params = {
        pageIndex: this.currentPage = nowPage,
        pageSize: this.pageSize,
        type:this.delegateType
      };
      this.showDelegateList(params);
    },
    prePage:function(nowPage){
      let params = {
        pageIndex: this.currentPage = this.currentPage - 1,
        pageSize: this.pageSize,
        type:this.delegateType
      };
      this.showDelegateList(params);
    },
    nextPage:function(nowPage){
      let params = {
        pageIndex: this.currentPage = this.currentPage + 1,
        pageSize: this.pageSize,
        type:this.delegateType
      };
      this.showDelegateList(params);
    },

    //判断是否有type参数
    urlHasParam:function(){
        return typeof(this.$router.query.type) != 'undefined';
    },
    transDate:function(row,col,value){  
      var y = value.year + 1900;
      var m = value.month + 1;
      var d = value.date;
      var h = value.hours;
      var mm = value.minutes;
      var s = value.seconds;
      return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s) ;
    },
    add0:function(m){
      return m<10?'0'+m:m 
    },
    transStatus:function(row,col,value){
      if(value){
        return "可用";
      }else{
        return "不可用";
      }
    },
    //删除代理用户设置
    delDelegate:function(row){
      let _this = this;
      this.$confirm(_this.$t('wd.isConfirmDeleted'),_this.$t('cm.tips'),{
        confirmButtonText: _this.$t('te.confirm'),
        cancelButtonText: _this.$t('te.cancel'),
        cancelButtonClass: 'btn-second',
        confirmButtonClass: 'btn-default',
      }).then(function(){
        deldelegateusersetting({pid:row.ID}).then((result) => {
          if(result.status == "200" && result.data.code == "0"){
              _this.initList();
          }else{
              _this.$alert(result.data.msg,_this.$t('cm.tips'));
          }
          }).catch((err) => {
              _this.$alert(err,_this.$t('cm.tips'));
          });                   
      }).catch({});
    },
    //初始化列表操作
    initList:function(){
      let params = {
        pageIndex:1,
        pageSize:this.pageSize,
        type:this.delegateType 
      };
      this.showDelegateList(params);
    },
    //编辑代理人
    editDelegate:function(row){
      this.$router.push({path:'/work_delegate/edit',query:{obj:row}});

    }
  },
  mounted() {
    //委托工作和受托工作 暂时共用一个data数据
    let params = {
      pageIndex:this.currentPage,
      pageSize:this.pageSize,
      type:this.delegateType  //1 表示委托 2表示受托
    }
    this.showDelegateList(params);
    
  },
}